local WndData = class("WndData");
function WndData:ctor()
    self.name = "";
	self.param = {};
	self.cls = nil;
	self.wnd = nil;
	self.wndToOpen = nil;
end

local WndMgr = class("WndMgr");
function WndMgr:ctor()
    self.wnds = {};
	self.layers = {};
	self.mainNode = nil;
	self.commonUI = nil;
    local visibleRect = cc.Director:getInstance():getOpenGLView():getVisibleRect();
    local x = visibleRect.x;
    local y = visibleRect.y;
    local width = visibleRect.width;
    local height = visibleRect.height;
    self.visibleRect = {
        x = x,
        y = y,
        width = width,
        height = height,
        size = cc.size(width,height),
        center = cc.p(x+width/2,y+height/2),
        top = cc.p(x+width/2,y+height),
        bottom = cc.p(x+width/2,y),
        left = cc.p(x,y+height/2),
        right = cc.p(x+width,y+height/2),
        leftTop = cc.p(x,y+height),
        rightTop = cc.p(x+width,y+height),
        leftBottom = cc.p(x,y),
        rightBottom = cc.p(x+width,y)
    }
    --dump(self.visibleRect,"visibleRect:");
end

function WndMgr:addMianNode(mainNode)
	self.layers = {};
	self.mainNode = mainNode;
    local child = self.mainNode:getChildren();
    for i,v in ipairs(child) do
        self.layers[i] = v;
    end
end

function WndMgr:getVisibleRect()
    return self.visibleRect;
end

function WndMgr:registerWnd(name,cls)
	print(name)
	if self.wnds[name] then
		mgrs.log:printError(string.format("????????%s??????????,??????enums.Wnd??????",name));
	else
		local d = WndData.new();
		d.cls = cls;
		d.name = name;
		self.wnds[name] = d;
	end
end	

function WndMgr:openWnd(name,param)
	local d = self:getWndData(name);
	if d and (d.wnd or d.wndToOpen) then
		return;
	end
	d.param = param;
	local cls = d.cls;
	d.wndToOpen = cls.new();--CCNode ????
	d.wndToOpen.wndData = d;
	d.wndToOpen:reqData(function(ok)
		if ok then
			d.wnd = d.wndToOpen;
			d.wndToOpen = nil;
			local par = self:getLayer(d.wnd:getLayer());
            d.wnd:setContentSize(display.size);--????node???��
            d.wnd:ignoreAnchorPointForPosition(false);
	        d.wnd:setAnchorPoint(cc.p(0.5,0.5));
            d.wnd:setPosition(self.visibleRect.center);
			par:addChild(d.wnd);
            d.wnd:openWnd();
		else
			d.wndToOpen = nil;
		end
	end)
end

function WndMgr:closeWnd(name)
	local d = self:getWndData(name);
	d.wndToOpen = nil;
	if not d.wnd then
		return;
	end
	d.wnd:close();
end

function WndMgr:getWndData(name)
	local d = self.wnds[name];
	if d then
		return d;
	end
	return nil;
end

function WndMgr:getWnd(name)
	return self:getWndData(name).wnd;
end

function WndMgr:getWndName(name)
	return self:getWndData(name).name;
end
	
function WndMgr:isOpen(name)
	return self:getWndData(name).wnd ~= nil;
end
function WndMgr:getLayer(idx)
	return self.layers[idx];
end

mgrs.wnd = nil;
mgrs.singleClass(mgrs,WndMgr,"wnd");